package com.dime.physical.service.impl;

import com.dime.basic.api.RemoteBaseCombService;
import com.dime.basic.api.RemoteOccupationalBasicService;
import com.dime.basic.api.domain.DimeBasediagnosisstandard;
import com.dime.basic.api.domain.DimeBaseevaluation;
import com.dime.basic.api.model.*;
import com.dime.physical.domain.DimeBasecompany;
import com.dime.physical.domain.DimePhysicalMrMain;
import com.dime.physical.domain.dto.DimePhysicalMrMainUpdateDto;
import com.dime.physical.enums.OccupationalMRMainReportStatus;
import com.dime.physical.mapper.DimePhysicalMrMainMapper;
import com.dime.physical.mapper.DimePhysicalOccupationalMapper;
import com.dime.physical.model.*;
import com.dime.physical.model.dto.OccupationHealthCheckEmployeesDto;
import com.dime.physical.model.dto.SearchEmployeeConditionDto;
import com.dime.physical.model.mrreport.*;
import com.dime.physical.service.IDimeBasecompanyService;
import com.dime.physical.service.IDimePhysicalOccupationalService;
import com.dime.physical.service.IDimePhysicalharmsService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.CheckedException;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.JsonUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.RemoteDimeSerialNumberService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.constant.DictConstant;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.api.model.UserShop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DimePhysicalOccupationalServiceImpl implements IDimePhysicalOccupationalService {
    private static final Logger log = LoggerFactory.getLogger(DimePhysicalOccupationalServiceImpl.class);

    public static void main(String[] args) {
//        List<String> list= Arrays.asList("a", "b", "c", "d");
        List<Integer> num = Arrays.asList(1,2,3,4,5);
//        List<String> collect =list.stream().map(x->x+"dd").collect(Collectors.toList());
//        List<Integer> collect =num.stream().map(x->x*2).collect(Collectors.toList());
//        List<Integer> collect =num.stream().filter(x->x>2).collect(Collectors.toList());
//        Integer integer = Optional.ofNullable(num.get(2)).filter(x -> x == 2).orElse(8);
//        List<Integer> integers = Optional.ofNullable(num).filter(x -> x.size() > 8).orElse(null);
//        List<Integer> s = num.stream().map(x -> x+1).collect(Collectors.toList());
//        System.out.println(s);
//        System.out.println(collect); //[A, B, C, D]

    }
    @Autowired
    private DimePhysicalOccupationalMapper occupationalMapper;

    @Autowired
    private RemoteOccupationalBasicService occupationalBasicService;

    @Autowired
    private IDimeBasecompanyService dimeBasecompanyService;

    @Autowired
    private IDimePhysicalharmsService dimePhysicalharmsService;

    @Autowired
    private RemoteDictService dictService;

    @Autowired
    private RemoteBaseCombService combService;

    @Autowired
    private RemoteDimeSerialNumberService serialNumberService;

    @Autowired
    private DimePhysicalMrMainMapper mrMainMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    public List<OccupationalEmployeeAnalysisVO> testgetemployeeAnalysis(OccupationHealthCheckEmployeesDto dto){
        return occupationalMapper.selectOccupationalConclusionTagsList(dto);
    }
    public List<OccupationalCheckMainVO> testgetemployees(OccupationHealthCheckEmployeesDto dto){
        return occupationalMapper.searchOccupationalEmployee(dto);
    }

    @Override
    public OccupationalMainVO getOccupationalHealthCheckById(String id) throws Exception{
        DimePhysicalMrMain main = mrMainMapper.selectDimePhysicalMrMainById(id);
        if(!StringUtils.isNotNull(main))throw new Exception("没有找到总检记录！");
        if(!StringUtils.isNotNull(main.getJsonData()))throw new Exception("总检记录数据不正确！");
        JsonUtils<OccupationalMainVO> jsonUtils = new JsonUtils<>(OccupationalMainVO.class);
        OccupationalMainVO vo = jsonUtils.jsonDeserialize(main.getJsonData());
        return vo;
    }



    public List<OccupationalCheckMainVO> searchOccupationalEmployee(SearchEmployeeConditionDto dto) {
        List<String> ids = new ArrayList<>();
        if(!StringUtils.isNotNull(dto)) throw new CheckedException("查询条件不能为空！");
        if(!StringUtils.isNotNull(dto.getWorkStatus()) || dto.getWorkStatus().size() == 0) throw new CheckedException("在岗状态不能为空！");
      return occupationalMapper.searchOccupationalEmployee(dto);
    }

    @Override
    public List<OccupationalCheckMainVO> searchOccupationalEmployeesByIds(List<String> ids) {
        return occupationalMapper.searchOccupationalEmployeesByIds(ids);
    }

    @Override
    public OccupationalMainVO occupationalHealthCheck(OccupationHealthCheckEmployeesDto dto) {
        R<OccupationalBasic> rOccuBasic = occupationalBasicService.occupationalBasic(SecurityConstants.INNER);
        if (!StringUtils.isNotNull(dto)) {
            throw new ServiceException("获取总检参数出错!");
        }
        if (!StringUtils.isNotNull(dto.getCompanyId())) {
            throw new ServiceException("公司不能为空!");
        }

        if ((!StringUtils.isNotNull(dto.getEmployees()) || dto.getEmployees().size() == 0) && !dto.isSelectAll()) {
            throw new ServiceException("总检人员不能为空!");
        }
        if (R.isError(rOccuBasic)) {
            log.error("获取总检基础信息出错!");
            throw new ServiceException("获取总检基础信息出错!");
        }
        OccupationalMainVO vo = new OccupationalMainVO();
        vo.setWorkStatus(dto.getWorkStatus());
        String shopCode = SecurityUtils.getShopId();
        R<UserShop>  rShop = remoteUserService.getUserShopByShopCode(shopCode, SecurityConstants.INNER);
        if(!R.isSuccess(rShop)){ throw new ServiceException(rShop.getMsg());}
        vo.setShop(rShop.getData());
        R<String>  r = serialNumberService.getOccupationalReportNo();
        if(!R.isSuccess(r)){ throw new ServiceException("获取报告编号出错!");}
        vo.setReportNo(r.getData());
        OccupationalBasic occupationalBasic = rOccuBasic.getData();
        List<OccupationalEmployeeAnalysisVO> ees;
        List<OccupationalCheckMainVO> checkMains;
        if (dto.isSelectAll()) {
            ees = occupationalMapper.selectOccupationalConclusionTagsList(dto);
            checkMains = occupationalMapper.searchOccupationalEmployee(dto);
        } else {
            ees = occupationalMapper.selectOccupationalConclusionTags(dto.getEmployees());
            checkMains = occupationalMapper.searchOccupationalEmployeesByIds(dto.getEmployees());
        }
        vo.setEes(ees);
        vo.setCheckMains(checkMains);
        fillStartAndEndDate(vo);
        DimeBasecompany basecompany = dimeBasecompanyService.selectDimeBasecompanyById(dto.getCompanyId());
        CompanyVO companyVO = new CompanyVO(basecompany.getId(), basecompany.getCompanyName());
        vo.setDelegateCompany(companyVO);
        vo.setCompany(companyVO);
        fillEmployeeHarms(checkMains);
        //添加关注
        vo.setFocusCount(checkMains.stream().filter(t -> t.getFocusTag()!= null && t.getFocusTag().equals("1")).count());
        //添加评价依据
        fillVOEvaluationBasis(vo, occupationalBasic);
        //添加诊断标准
        fillVOOcptDiagnoseStandard(vo, occupationalBasic);
        fillVOHarmWorkStatusGroupAnalysis(vo, occupationalBasic);
        fillVOHarmWorkStatusAnalysis(vo,occupationalBasic);
        fillHarmAnalysis(vo);
        fillAttachment(vo);
        fillbcitems(vo);
        return vo;
    }

    // 添加开始日期和结束日期
    private void fillStartAndEndDate(OccupationalMainVO vo){
        try {
            Date enddate = vo.getCheckMains().stream()
                    .max(Comparator.comparing(OccupationalCheckMainVO::getRegisterDate)).get().getCheckDate();

            Date startdate = vo.getCheckMains().stream()
                    .min(Comparator.comparing(OccupationalCheckMainVO::getRegisterDate)).get().getCheckDate();
            vo.setCheckDateStart(startdate);
            vo.setCheckDateEnd(enddate);
        }catch (Exception ex){
            throw new ServiceException(ex.getMessage());
        }
    }
    private void fillEES(List<OccupationalCheckMainVO> ees) {
        R<List<SysDictData>> rdicts = dictService.getDictType(DictConstant.DICT_WORKSTATUS);
        List<SysDictData> dicts = rdicts.getData();
        Map<String, String> mapDicts = dicts.stream().
                collect(Collectors.toMap(key -> key.getDictValue(), value -> value.getDictLabel()));
        for (OccupationalCheckMainVO ee : ees
        ) {
            ee.setWorkStatusName(mapDicts.get(ee.getWorkStatus()));
        }
    }

    // 添加人员健康危害
    private void fillEmployeeHarms(List<OccupationalCheckMainVO> ees) {
        if (ees != null && ees.size() == 0) return;
        for (OccupationalCheckMainVO ee : ees
        ) {
            String harmsStr = String.join(",", ee.getHarmVOS().stream()
                    .sorted(Comparator.comparing(OccupationalHarmVO::getHarmId)
                            .thenComparing(OccupationalHarmVO::getHarmName))
                    .map(r -> r.getHarmName()).collect(Collectors.toList()));
            ee.setHarmStr(harmsStr);
        }
    }
    //添加评价依据
    private void fillVOEvaluationBasis(OccupationalMainVO vo, OccupationalBasic basic) {
        if (basic != null) {
            if(basic.getEvaluationBasics() != null && basic.getEvaluationBasics().size() > 0){
               List<String> evaluationBasics =  basic.getEvaluationBasics()
                        .stream()
                        //.sorted(Comparator.comparing(DimeBaseevaluation::getCode))
                        .map(DimeBaseevaluation::getName)
                        .collect(Collectors.toList());
               vo.setEvaluationBasis(evaluationBasics);
            }
        }
    }
    //添加诊断标准
    private void fillVOOcptDiagnoseStandard(OccupationalMainVO vo, OccupationalBasic basic) {
        if (basic != null) {
            if(basic.getDiagnosisStandardBasics() != null && basic.getDiagnosisStandardBasics().size() > 0){
                List<String> diagnoseStandards =  basic.getDiagnosisStandardBasics()
                        .stream()
                        .sorted(Comparator.comparing(DimeBasediagnosisstandard::getCode))
                        .map(DimeBasediagnosisstandard::getName)
                        .collect(Collectors.toList());
                vo.setDiagnoseStandards(diagnoseStandards);
            }
        }
    }
    // 添加
    private void fillbcitems(OccupationalMainVO main) {
        if(!StringUtils.isNotNull(main.getCheckMains()))return;
        List<String> combNames = new ArrayList<>();
        for (OccupationalCheckMainVO checkmainvo:main.getCheckMains()
             ) {
           for(OccupationalCheckItemVO checkitem :checkmainvo.getCheckItems()){
               if(!combNames.stream().anyMatch(t->t.equals(checkitem.getCombName()))){
                   combNames.add(checkitem.getCombName());
               }
           }
        }
        main.setBcitems(combNames.stream().collect(Collectors.joining(",")));
    }
    private void fillVOHarmWorkStatusGroupAnalysis(OccupationalMainVO vo, OccupationalBasic basic) {
        Map<String, List<OccupationalCheckMainVO>> map = vo.getCheckMains().stream()
                .collect(Collectors.groupingBy(ord ->  ord.getHarmStr() +"|" + ord.getWorkStatus() + "|" + ord.getWorkStatusName()));

        List<OccupationalHarmWorkStatusGroupVO> harmWorkstatusGroups = new ArrayList<>();
        List<OccupationalEmployeeAnalysisVO> conclusionEEs =  vo.getEes();
        for (String key : map.keySet()) {
            String[] harmWorkStatus = key.split("\\|");
            OccupationalHarmWorkStatusGroupVO group = new OccupationalHarmWorkStatusGroupVO();
            group.setHarmfactor(harmWorkStatus[0]);
            group.setWorkStatus(harmWorkStatus[1]);
            group.setWorkStatusName(harmWorkStatus[2]);
            group.setEmployees(map.get(key));
            List<String> harmIds = new ArrayList<>();
            List<OccupationalCheckMainVO> ees = map.get(key);
            List<HarmCombo> harmCombos = new ArrayList<>();
            if (!ees.isEmpty() && ees.size() > 0) {
                harmIds = ees.get(0).getHarmVOS().stream().map(t -> t.getHarmId())
                        .collect(Collectors.toList());
            }
            if (!harmIds.isEmpty() && harmIds.size() > 0) {
                R<List<HarmCombo>> rharmCombos = combService.getComboByHarmIds(harmIds.toArray(new String[harmIds.size()]), SecurityConstants.INNER);
                if (R.isSuccess(rharmCombos)) {
                    harmCombos = rharmCombos.getData();
                }
            }
            if (!harmCombos.isEmpty() && harmCombos.size() > 0) {
                List<String> reportCheckItems = harmCombos.stream().map(t -> t.getCombName()).distinct().collect(Collectors.toList());
                if (!reportCheckItems.isEmpty() && reportCheckItems.size() > 0)
                    group.setReportCheckItem(reportCheckItems);
                List<String> chosenCheckItems = harmCombos.stream().filter(t -> t.getRequired() == 0).map(t -> t.getCombName()).distinct().collect(Collectors.toList());
                if (!chosenCheckItems.isEmpty() && chosenCheckItems.size() > 0)
                    group.setChosenCheck(chosenCheckItems);
                List<String> mustCheckItems = harmCombos.stream().filter(t -> t.getRequired() == 1).map(t -> t.getCombName()).distinct().collect(Collectors.toList());
                if (!mustCheckItems.isEmpty() && mustCheckItems.size() > 0)
                    group.setMustCheck(mustCheckItems);
            }
            group.setNumber(ees.size());
            //filter conclusions

          /*  List<OccupationalEmployeeAnalysisVO> workstatusEEs = conclusionEEs.stream().filter(t->t.getWorkStatus().equals(group.getWorkStatus())).collect(Collectors.toList());
            if(StringUtils.isNotNull(workstatusEEs)) {
                for (OccupationalEmployeeAnalysisVO ee : workstatusEEs
                ) {
                    if (StringUtils.isNotNull(ee.getHarmConclusions()) && ee.getHarmConclusions().size() > 0) {
                        for (OccupationalHarmConclusionAnalysisVO harmconclusion : ee.getHarmConclusions()
                        ) {
                              if(  harmconclusion.getHarmId().equals(group.getHarmfactor())) {

                                  group.setHarmId(harmconclusion.getHarmId());
                                  group.setHarmName(harmconclusion.getHarmName());
                                  int eeCount = harmAnalysisVO.getEeCount();
                                  eeCount += 1;
                                  if (StringUtils.isNotNull(harmconclusion.getConclusions())) {
                                      int contraEeCount = harmAnalysisVO.getContraEeCount();
                                      if (StringUtils.isNotNull(harmconclusion.getConclusions().getContraindicationTag()) &&
                                              harmconclusion.getConclusions().getContraindicationTag().equals("1")
                                      ) {
                                          contraEeCount += 1;
                                      }
                                      harmAnalysisVO.setContraEeCount(contraEeCount);
                                      int diseaseEeCount = harmAnalysisVO.getDiseaseEeCount();
                                      if (StringUtils.isNotNull(harmconclusion.getConclusions().getOccupationalTag()) &&
                                              harmconclusion.getConclusions().getOccupationalTag().equals("1")
                                      ) {
                                          diseaseEeCount += 1;
                                      }
                                      harmAnalysisVO.setContraEeCount(diseaseEeCount);
                                      int otherEeCount = harmAnalysisVO.getOtherEeCount();
                                      if (StringUtils.isNotNull(harmconclusion.getConclusions().getOtherTag()) &&
                                              harmconclusion.getConclusions().getOtherTag().equals("1")
                                      ) {
                                          otherEeCount += 1;
                                      }
                                      harmAnalysisVO.setOtherEeCount(otherEeCount);

                                      int passEeCount = harmAnalysisVO.getPassEeCount();
                                      if (StringUtils.isNotNull(harmconclusion.getConclusions().getPassTag()) &&
                                              harmconclusion.getConclusions().getPassTag() == 1
                                      ) {
                                          passEeCount += 1;
                                      }
                                      harmAnalysisVO.setPassEeCount(passEeCount);

                                      List<String> notPassEeNames = harmAnalysisVO.getNotPassEeNames();
                                      if ((!StringUtils.isNotNull(harmconclusion.getConclusions().getPassTag()) ||
                                              harmconclusion.getConclusions().getPassTag() != 1)
                                      ) {
                                          if (!StringUtils.isNotNull(notPassEeNames))
                                              notPassEeNames = new ArrayList<>();
                                          if (!notPassEeNames.contains(ee.getName())) notPassEeNames.add(ee.getName());
                                      }
                                      harmAnalysisVO.setNotPassEeNames(notPassEeNames);
                                  }
                                  harmAnalysisVO.setEeCount(eeCount);
                              }
                        }
                    }
                }
            }*/
            harmWorkstatusGroups.add(group);
        }
        vo.setHarmWorkStatusGroupAnalysises(harmWorkstatusGroups);
    }
    private void fillVOHarmWorkStatusAnalysis(OccupationalMainVO vo, OccupationalBasic basic){
        Map<String,String> dictWorkStatus = getWorkStatus();
        for (OccupationalHarmWorkStatusGroupVO group:
        vo.getHarmWorkStatusGroupAnalysises()) {
            ArrayList<HarmWorkStatus> harmWorkStatuses = getharmWorkStatus(group.getEmployees());
            if(StringUtils.isNotNull(harmWorkStatuses) && harmWorkStatuses.size() > 0) {
                R<List<HarmContraCriterion>> rhcc = occupationalBasicService.harmContraCriterionsByList(harmWorkStatuses, SecurityConstants.INNER);
                if (R.isSuccess(rhcc)) {
                    List<HarmContraCriterion> contraCriterias = rhcc.getData();
                    group.setContras(contraCriterias);
                }
                R<List<HarmDiseaseCriterion>> rhdc = occupationalBasicService.harmDiseaseCriterionsByList(harmWorkStatuses, SecurityConstants.INNER);
                if (R.isSuccess(rhcc)) {
                    List<HarmDiseaseCriterion> diseaseCriterias = rhdc.getData();
                    group.setDiseaseCriterions(diseaseCriterias);
                }
            }
        }
    }
    private void fillResultDesc(OccupationalMainVO vo, OccupationalBasic basic){

    }
    private ArrayList<HarmWorkStatus> getharmWorkStatus(List<OccupationalCheckMainVO> ees) {
        if (ees == null || ees.size() == 0) throw new CheckedException("体检人员列表为空！");
        ArrayList<HarmWorkStatus> result = new ArrayList<>();
        for (OccupationalCheckMainVO ee : ees
        ) {
            for (OccupationalHarmVO harm : ee.getHarmVOS()) {
                boolean f = false;
                for (HarmWorkStatus hw : result
                ) {
                    if(harm.getHarmId().equals(hw.getHarmId()) && ee.getWorkStatus().equals(hw.getWorkStatus())){
                        f = true;
                        break;
                    }
                }
                if(!f) {
                    result.add(
                            HarmWorkStatus
                                    .builder()
                                    .harmId(harm.getHarmId())
                                    .harmName(harm.getHarmName())
                                    .workStatus(ee.getWorkStatus())
                                    .build()
                    );
                }
            }
        }
        return result;
    }
    private Map<String, String> getWorkStatus() {
        R<List<SysDictData>> rdicts = dictService.getDictType(DictConstant.DICT_WORKSTATUS);
        List<SysDictData> dicts = rdicts.getData();
        Map<String, String> mapDicts = dicts.stream().
                collect(Collectors.toMap(key -> key.getDictValue(), value -> value.getDictLabel()));
        return mapDicts;
    }
    private void fillHarmAnalysis(OccupationalMainVO vo){
       List<OccupationalEmployeeAnalysisVO> ees =  vo.getEes();
       List<OccupationalHarmAnalysisVO> harmAnalysisVOS = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
             ) {
            if(StringUtils.isNotNull(ee.getHarmConclusions()) && ee.getHarmConclusions().size() >0){
                for (OccupationalHarmConclusionAnalysisVO harmconclusion:ee.getHarmConclusions()
                     ) {
                    if(StringUtils.isNotNull(harmconclusion) && StringUtils.isNotNull(harmconclusion.getHarmId())){
                       Optional<OccupationalHarmAnalysisVO> optHarmAnalysisVO = harmAnalysisVOS.stream().filter(t->t.getHarmId().equals(harmconclusion.getHarmId())).findFirst();
                        OccupationalHarmAnalysisVO harmAnalysisVO;
                       if(!optHarmAnalysisVO.isPresent())
                        {
                            harmAnalysisVO = new OccupationalHarmAnalysisVO();
                            harmAnalysisVO.setHarmId(harmconclusion.getHarmId());
                            harmAnalysisVO.setHarmName(harmconclusion.getHarmName());
                            int eeCount = harmAnalysisVO.getEeCount();
                            eeCount += 1;
                            if(StringUtils.isNotNull(harmconclusion.getConclusions())){
                                int contraEeCount = harmAnalysisVO.getContraEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getContraindicationTag()) &&
                                        harmconclusion.getConclusions().getContraindicationTag().equals("1")
                                ){
                                    contraEeCount += 1;
                                }
                                harmAnalysisVO.setContraEeCount(contraEeCount);
                                int diseaseEeCount = harmAnalysisVO.getDiseaseEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getOccupationalTag()) &&
                                        harmconclusion.getConclusions().getOccupationalTag().equals("1")
                                ){
                                    diseaseEeCount += 1;
                                }
                                harmAnalysisVO.setContraEeCount(diseaseEeCount);
                                int otherEeCount = harmAnalysisVO.getOtherEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getOtherTag()) &&
                                        harmconclusion.getConclusions().getOtherTag().equals("1")
                                ){
                                    otherEeCount += 1;
                                }
                                harmAnalysisVO.setOtherEeCount(otherEeCount);

                                int passEeCount = harmAnalysisVO.getPassEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getPassTag()) &&
                                        harmconclusion.getConclusions().getPassTag() == 1
                                ){
                                    passEeCount += 1;
                                }
                                harmAnalysisVO.setPassEeCount(passEeCount);

                                List<String> notPassEeNames = harmAnalysisVO.getNotPassEeNames();
                                if((!StringUtils.isNotNull(harmconclusion.getConclusions().getPassTag()) ||
                                        harmconclusion.getConclusions().getPassTag() != 1)
                                ){
                                    if(!StringUtils.isNotNull(notPassEeNames))notPassEeNames = new ArrayList<>();
                                    if(!notPassEeNames.contains(ee.getName())) notPassEeNames.add(ee.getName());
                                }
                                harmAnalysisVO.setNotPassEeNames(notPassEeNames);
                            }
                            harmAnalysisVO.setEeCount(eeCount);

                            harmAnalysisVOS.add(harmAnalysisVO);
                        }else{
                            harmAnalysisVO = optHarmAnalysisVO.get();
                            int eeCount = harmAnalysisVO.getEeCount();
                            eeCount += 1;
                            if(StringUtils.isNotNull(harmconclusion.getConclusions())){
                                int contraEeCount = harmAnalysisVO.getContraEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getContraindicationTag()) &&
                                        harmconclusion.getConclusions().getContraindicationTag().equals("1")
                                ){
                                    contraEeCount += 1;
                                }
                                harmAnalysisVO.setContraEeCount(contraEeCount);
                                int diseaseEeCount = harmAnalysisVO.getDiseaseEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getOccupationalTag()) &&
                                        harmconclusion.getConclusions().getOccupationalTag().equals("1")
                                ){
                                    diseaseEeCount += 1;
                                }
                                harmAnalysisVO.setContraEeCount(diseaseEeCount);
                                int otherEeCount = harmAnalysisVO.getOtherEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getOtherTag()) &&
                                        harmconclusion.getConclusions().getOtherTag().equals("1")
                                ){
                                    otherEeCount += 1;
                                }
                                harmAnalysisVO.setOtherEeCount(otherEeCount);
                                int passEeCount = harmAnalysisVO.getPassEeCount();
                                if(StringUtils.isNotNull(harmconclusion.getConclusions().getPassTag()) &&
                                        harmconclusion.getConclusions().getPassTag() == 1
                                ){
                                    passEeCount += 1;
                                }
                                harmAnalysisVO.setPassEeCount(passEeCount);
                                List<String> notPassEeNames = harmAnalysisVO.getNotPassEeNames();
                                if((!StringUtils.isNotNull(harmconclusion.getConclusions().getPassTag()) ||
                                        harmconclusion.getConclusions().getPassTag() != 1)
                                ){
                                    if(!StringUtils.isNotNull(notPassEeNames))notPassEeNames = new ArrayList<>();
                                    if(!notPassEeNames.contains(ee.getName())) notPassEeNames.add(ee.getName());
                                }
                                harmAnalysisVO.setNotPassEeNames(notPassEeNames);
                            }
                            harmAnalysisVO.setEeCount(eeCount);
                        }
                       if(StringUtils.isNotNull(ee.getHarmCombs())){
                           List<OccupationalComboAnalysisVO> comboAnalysisVOS = harmAnalysisVO.getComboAnalysises();
                          Optional<OccupationalHarmCombVO> optCombos = ee.getHarmCombs().stream().filter(t->harmAnalysisVO.getHarmId().equals(t.getHarmId())).findFirst();
                          if(optCombos.isPresent()) {
                              if(!StringUtils.isNotNull(comboAnalysisVOS))comboAnalysisVOS = new ArrayList<>();
                                List<OccupationalComboVO> combos = optCombos.get().getCombos();
                              for (OccupationalComboVO combo:combos
                                   ) {
                                 Optional<OccupationalComboAnalysisVO> optComboAnalysis = comboAnalysisVOS.stream().filter(t->combo.getCombId().equals(t.getCombId())).findFirst();
                                    if(optComboAnalysis.isPresent()){
                                        optComboAnalysis.get().setCount(optComboAnalysis.get().getCount() + 1);
                                    }else{
                                        OccupationalComboAnalysisVO comboAnalysisVO = new OccupationalComboAnalysisVO();
                                        comboAnalysisVO.setCount(1);
                                        comboAnalysisVO.setCombId(combo.getCombId());
                                        comboAnalysisVO.setCombName(combo.getCombName());
                                        comboAnalysisVOS.add(comboAnalysisVO);
                                    }
                              }
                          }
                          harmAnalysisVO.setComboAnalysises(comboAnalysisVOS);
                       }
                    }
                }
            }
        }
        if(harmAnalysisVOS.size() > 0){
            int i = 0;
            for (OccupationalHarmAnalysisVO harmanalysis: harmAnalysisVOS
                 ) {
                List<String> str = new ArrayList<>();
                i++;
                int j = 1;
                str.add(StringUtils.format(
                        "{}、本次接触\"{}\"作业人员的职业健康检查，实际检查{}人次，统计分析{}人次，",
                        i, //StringUtils.int2chineseNum(i),
                        harmanalysis.getHarmName(),
                        harmanalysis.getEeCount(),
                        harmanalysis.getEeCount()));
                str.add("结果如下:");
                str.add(StringUtils.format("{}.体检结果无明显异常{}人次，占统计分析人次数的{}。",
                        j,harmanalysis.getPassEeCount(), (harmanalysis.getPassEeCount()/harmanalysis.getEeCount())*100 + "%"
                        ));
                j++;
                str.add(StringUtils.format("{}.检查项目异常情况{}人次(异常人员清单详见附件),占统计分析人次数的{}。",
                        j,harmanalysis.getEeCount() - harmanalysis.getPassEeCount(), ((harmanalysis.getEeCount() - harmanalysis.getPassEeCount())/harmanalysis.getEeCount())*100 + "%"
                ));
                j++;
                if(harmanalysis.getEeCount() != harmanalysis.getPassEeCount()){
                    str.add("检查项目异常情况：");
                    if(StringUtils.isNotNull(harmanalysis.getComboAnalysises())) {

                      String strNotPass =  harmanalysis
                                .getComboAnalysises()
                                .stream()
                                .map(t-> StringUtils.format("[{}]检查异常{}人次", t.getCombName(),t.getCount()))
                                .collect(Collectors.joining("，")) + "。";
                      str.add(strNotPass);


                    }
                }
                harmanalysis.setHarmAnalysisStr(str.stream().collect(Collectors.joining("\n")));
            }
        }
        vo.setHarmAnalysises(harmAnalysisVOS);
    }
    @Override
    public String occupationalHealthCheckSave(OccupationalMainVO dto) throws Exception {
        DimePhysicalMrMain main = new DimePhysicalMrMain();
        Date now = new Date();
        String id = UUID.randomUUID().toString();
        dto.setId(id);
        main.setId(id);
        main.setCreateTime(now);
        main.setCreateBy(SecurityUtils.getUsername());
        main.setReportno(dto.getReportNo());
        main.setOrgcheck(dto.getShop().getShopName());
        main.setApprovecert("");
        main.setApprovecert2("");
        main.setBctype("");
        main.setCompanyid(dto.getCompany().getId());
        main.setCompanyname(dto.getCompany().getName());
        main.setEntrustcompany(dto.getCompany().getName());
        main.setOthercompany("");
        main.setOthercompanyname("");
        main.setWorkstatus(dto.getHarmWorkStatusGroupAnalysises().get(0).getWorkStatus());
        main.setBcstart(dto.getCheckDateStart());
        main.setBcend(dto.getCheckDateEnd());
        main.setEvalutionbasis("");
        main.setOcptddiagnosestandard("");
        main.setCrowdharmfactor("");
        main.setResultdesc("");
        main.setResultanalysis("");
        main.setProcessadvice("");
        main.setReportmaker(SecurityUtils.getUsername());
        main.setMakedate(now);


        main.setStatus(OccupationalMRMainReportStatus.OK.getCode());
        main.setShopCode(SecurityUtils.getShopId());
        main.setOrgAddress(Optional.ofNullable(dto).map(s->s.getShop()).map(a->a.getAddress()).orElse(""));
        main.setOrgContact(Optional.ofNullable(dto).map(s->s.getShop()).map(a->a.getContact()).orElse(""));
      //  main.setOrgContactphone(Optional.ofNullable(dto).map(s->s.getShop()).map(a->a.).orElse(""));
        String jsonData = "";
        try{
            JsonUtils<OccupationalMainVO> jsonUtils = new JsonUtils<>(OccupationalMainVO.class);
            jsonData =  jsonUtils.jsonSerialize(dto);
        }catch (Exception ex){
            throw ex;
        }
        main.setJsonData(jsonData);

        mrMainMapper.insertDimePhysicalMrMain(main);


        return id;
    }
    @Override
    public String occupationalHealthCheckApprove(OccupationalMainVO dto) throws Exception {
        Date now = new Date();
        DimePhysicalMrMain mrmain = mrMainMapper.selectDimePhysicalMrMainById(dto.getId());
        if(!StringUtils.isNotNull(mrmain))throw new CheckedException("没有找到报告！");
        if(!mrmain.getStatus().equals(OccupationalMRMainReportStatus.Creating.getCode()))throw new CheckedException("报告正在创建中！");
        mrmain.setStatus(OccupationalMRMainReportStatus.Published.getCode());
        mrmain.setReportapporver(SecurityUtils.getUsername());
        mrmain.setReportapprovedate(now);
        mrmain.setReportsigner(SecurityUtils.getUsername());
        mrmain.setSigndate(now);
        int r = mrMainMapper.updateDimePhysicalMrMain(mrmain);
        if(r >0) return mrmain.getId();
        else throw new Exception("更新失败！" +r+" 条数据被更新");
    }
    @Override
    @Transactional
    public String occupationalHealthCheckApproveByIds(String[] ids) throws Exception {
        if(!StringUtils.isNotNull(ids) || ids.length == 0)throw new CheckedException("请求报告为空！");
        Date now = new Date();
        List<DimePhysicalMrMain> mrmains = mrMainMapper.selectDimePhysicalMrMainByIds(ids);
        if(!StringUtils.isNotNull(mrmains))throw new CheckedException("没有找到报告！");
        DimePhysicalMrMainUpdateDto dto = new DimePhysicalMrMainUpdateDto();

       dto.setIds(
               mrmains.stream().map(DimePhysicalMrMain::getId).toArray(String[]::new)
       );




        dto.setStatus(OccupationalMRMainReportStatus.Published.getCode());
        dto.setReportapporver(SecurityUtils.getUsername());
        dto.setReportapprovedate(now);
        dto.setReportsigner(SecurityUtils.getUsername());
        dto.setSigndate(now);

        int r = mrMainMapper.updateDimePhysicalMrMainByIds(dto);
        if(r >0) return Arrays.stream(dto.getIds()).collect(Collectors.joining(","));
        else throw new Exception("更新失败！" +r+" 条数据被更新");
    }
    private void fillAttachment(OccupationalMainVO vo){
        if(!StringUtils.isNotNull(vo))return;
        if(!StringUtils.isNotNull(vo.getEes()))return;
        if(!StringUtils.isNotNull(vo.getAttachment())) vo.setAttachment(new MRAttachmentVO());
        vo.getAttachment().setRelateds(getRelateds(vo.getEes()));
        vo.getAttachment().setAsapComposites(getAsapComposites(vo.getEes()));
        vo.getAttachment().setContradictionExceptions(getContradictionExceptions(vo.getEes(),vo.getHarmAnalysises()));
        vo.getAttachment().setContradictions(getContradictions(vo.getHarmWorkStatusGroupAnalysises()));
        vo.getAttachment().setDiseaseExceptions(getDiseaseExceptions(vo.getEes(),vo.getHarmAnalysises()));
        vo.getAttachment().setHarmEffects(getHarmEffects(vo.getHarmWorkStatusGroupAnalysises()));
        vo.getAttachment().setItemLackPersons(getItemLackPersons(vo.getCheckMains()));
        vo.getAttachment().setOccupationTabooPeoples(getTabooPeoples(vo.getEes()));
        vo.getAttachment().setResultExceptions(getResultExceptions(vo.getEes(),vo.getHarmAnalysises()));
        vo.getAttachment().setSpecialRecheckPersons(getSpecialRecheckPersons(vo.getEes()));
        vo.getAttachment().setSuspectedOccupationalDiseasePeoples(getSuspectedOccupationalDiseasePeoples(vo.getEes()));
    }
    private List<MRRelatedVO> getRelateds(List<OccupationalEmployeeAnalysisVO> ees){
        List<MRRelatedVO> list = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
             ) {
            if(StringUtils.isNotNull(ee.getCheckMains())) {
                for (OccupationalCheckMainAnalysisVO main:
                        ee.getCheckMains()) {
                    if(!StringUtils.isNotNull(main))continue;
                    if(!StringUtils.isNotNull(main.getConclusions()))continue;
                    MRRelatedVO vo = new MRRelatedVO();
                    vo.setBcno(main.getCheckCode());
                    vo.setConclusion(main.getConclusions().getConclusion());
                    vo.setHarmfactor(ee.getHarmStr());
                    vo.setDepartment(ee.getDept());
                    vo.setPersonName(ee.getName());
                    vo.setSex(ee.getSex());
                    vo.setResult(main.getConclusions().getAbnormalresult());
                    list.add(vo);
                }
            }
        }
        return list;
    }
    private List<MRASAPCompositeVO>  getAsapComposites(List<OccupationalEmployeeAnalysisVO> ees){
        List<MRASAPCompositeVO> list = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
        ) {
            if(StringUtils.isNotNull(ee.getCheckMains())) {
                for (OccupationalCheckMainAnalysisVO main:
                        ee.getCheckMains()) {
                    if(!StringUtils.isNotNull(main))continue;
                    if(!StringUtils.isNotNull(main.getConclusions()))continue;
                    if(!"1".equals(main.getConclusions().getFocusTag()))continue;
                    MRASAPCompositeVO vo = new MRASAPCompositeVO();
                    vo.setCheckCode(main.getCheckCode());
                    vo.setEmpName(ee.getName());
                    vo.setHandleIdea(main.getConclusions().getConclusionadvice());
                    vo.setIdNO(ee.getIdNo());
                    vo.setMainConclusion(main.getConclusions().getConclusion());
                    vo.setHandleIdea(main.getConclusions().getConclusionadvice());
                    List<String> combName = new ArrayList<>();
                    List<String> groupName = new ArrayList<>();
                    for (OccupationalHarmConclusionAnalysisVO harmConclusion:
                         ee.getHarmConclusions()) {
                        if(!StringUtils.isNotNull(harmConclusion.getConclusions()))continue;
                        for (OccupationalHarmConclusionComboGroupVO combGroup:
                                harmConclusion.getConclusions().getRecheckComboGroup()
                             ) {
                            if(StringUtils.isNotNull(combGroup.getCombName())) {
                                if (!combName.contains(combGroup.getCombName())) {
                                    combName.add(combGroup.getCombName());
                                }
                            }
                            if(StringUtils.isNotNull(combGroup.getGroupName())) {
                                if (!groupName.contains(combGroup.getGroupName())) {
                                    groupName.add(combGroup.getGroupName());
                                }
                            }
                        }
                    }

                    vo.setListComboGuid(combName.stream().collect(Collectors.joining("，")));
                    vo.setListGroupGuid(groupName.stream().collect(Collectors.joining("，")));
                    list.add(vo);
                }
            }
        }
        return list;
    }
    private List<MRContradictionExceptionVO>  getContradictionExceptions(List<OccupationalEmployeeAnalysisVO> ees,List<OccupationalHarmAnalysisVO> harmAnalysisVOs){
        List<MRContradictionExceptionVO> list = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
        ) {
            if(StringUtils.isNotNull(ee.getCheckMains())) {
                for (OccupationalCheckMainAnalysisVO main:
                        ee.getCheckMains()) {
                    if(!StringUtils.isNotNull(main))continue;
                    if(!StringUtils.isNotNull(main.getConclusions()))continue;
                    if(!"1".equals(main.getConclusions().getContraindicationTag()))continue;
                    for (OccupationalHarmConclusionAnalysisVO harmconclusion:ee.getHarmConclusions()
                         ) {
                        //填充禁忌症人员列表
                        if(StringUtils.isNotNull(harmconclusion)){
                            if(StringUtils.isNotNull(harmconclusion.getConclusions())){
                              if("1".equals(harmconclusion.getConclusions().getContraindicationTag()))
                              {
                                for(OccupationalHarmContraindictionVO contraindictionVO :
                                  harmconclusion.getConclusions().getContraindictions()){
                                    String contraindictionName = Optional.ofNullable(contraindictionVO).isPresent() ? contraindictionVO.getContraindicationName() : "";
                                    String harmId = harmconclusion.getHarmId();
                                    String harmName = harmconclusion.getHarmName();
                                    Optional<MRContradictionExceptionVO> oVo = list.stream().filter(t -> contraindictionName.equals(t.getContraindication())
                                            && harmId.equals(t.getHarmId())
                                    ).findFirst();
                                    MRContradictionExceptionVO vo;
                                    if(oVo.isPresent()){
                                        vo = oVo.get();
                                    }else{
                                        vo = new MRContradictionExceptionVO();
                                        list.add(vo);
                                    }
                                    vo.setHarmId(harmId);
                                    vo.setHarmName(harmName);
                                    vo.setContraindication(contraindictionName);
                                    if(!StringUtils.isNotNull(vo.getIdNOs())){
                                        vo.setIdNOs(new ArrayList<>());
                                        vo.setPersonNames(new ArrayList<>());
                                    }
                                    if(!vo.getIdNOs().equals(ee.getIdNo()))        {
                                        vo.getIdNOs().add(ee.getIdNo());
                                        vo.getPersonNames().add(ee.getName());
                                    }

                                }
                              }
                            }
                        }
                    }
                }
            }
        }
        for (MRContradictionExceptionVO vo:
        list) {
           Optional<OccupationalHarmAnalysisVO> oharmvo =
                   harmAnalysisVOs.stream().filter(t->t.getHarmId().equals(vo.getHarmId())).findFirst();
           if(oharmvo.isPresent()) {
               vo.setHarmEeCount(oharmvo.get().getEeCount());
           }
           if(vo.getHarmEeCount() > 0){
               double dcontraEeCount = vo.getPersonNames().size();
               double dEeCount = vo.getHarmEeCount();
               NumberFormat nf = NumberFormat.getPercentInstance();
               nf.setMaximumFractionDigits(2);
                String rate = nf.format(dcontraEeCount/dEeCount);
               vo.setExceptionRate(rate);
           }
        }
        return list;
    }
    private List<MRContradictionVO> getContradictions(List<OccupationalHarmWorkStatusGroupVO> harmWorkstatusGroups){
        List<MRContradictionVO> list = new ArrayList<>();
        for (OccupationalHarmWorkStatusGroupVO hw:harmWorkstatusGroups
        ) {
            if(!StringUtils.isNotNull(hw.getContras()))continue;
            for (HarmContraCriterion harmContra:hw.getContras()
                 ) {
                for(Contraindication contra : harmContra.getContras()) {
                    if(!StringUtils.isNotNull(contra))continue;
                    if(list.stream().filter(t -> t.getHarmfactorId().equals(harmContra.getHarmId())
                            && t.getWorkstatus().equals(harmContra.getWorkStatus())
                            && t.getContradictionId().equals(contra.getContraId())
                    ).count() == 0){
                        MRContradictionVO vo = new MRContradictionVO();
                        vo.setContradiction(contra.getContraName());
                        vo.setContradictionId(contra.getContraId());
                        vo.setHarmfactor(harmContra.getHarmName());
                        vo.setHarmfactorId(harmContra.getHarmId());
                        vo.setWorkstatus(harmContra.getWorkStatus());
                        vo.setWorkstatusName(harmContra.getWorkStatusName());
                        list.add(vo);
                    }
                }
            }
        }
        return list;
    }
    private List<MRDiseaseExceptionVO>  getDiseaseExceptions(List<OccupationalEmployeeAnalysisVO> ees,List<OccupationalHarmAnalysisVO> harmAnalysisVOs){
        List<MRDiseaseExceptionVO> list = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
        ) {
            if(StringUtils.isNotNull(ee.getCheckMains())) {
                for (OccupationalCheckMainAnalysisVO main:
                        ee.getCheckMains()) {
                    if(!StringUtils.isNotNull(main))continue;
                    if(!StringUtils.isNotNull(main.getConclusions()))continue;
                    if(!"1".equals(main.getConclusions().getOccupationalTag()))continue;
                    for (OccupationalHarmConclusionAnalysisVO harmconclusion:ee.getHarmConclusions()
                    ) {
                        //填充禁忌症人员列表
                        if(StringUtils.isNotNull(harmconclusion)){
                            if(StringUtils.isNotNull(harmconclusion.getConclusions())){
                                if("1".equals(harmconclusion.getConclusions().getOccupationalTag()))
                                {
                                    for(OccupationalHarmDiseaseVO diseaseVO :
                                            harmconclusion.getConclusions().getDiseases()){
                                        String diseaseName = diseaseVO.getDiseaseName();
                                        String diseaseId = diseaseVO.getDiseaseId();
                                        String harmId = harmconclusion.getHarmId();
                                        String harmName = harmconclusion.getHarmName();
                                        Optional<MRDiseaseExceptionVO> oVo = list.stream().filter(t -> diseaseName.equals(t.getDiseaseName())
                                                && harmId.equals(t.getHarmId())
                                        ).findFirst();
                                        MRDiseaseExceptionVO vo;
                                        if(oVo.isPresent()){
                                            vo = oVo.get();
                                        }else{
                                            vo = new MRDiseaseExceptionVO();
                                            list.add(vo);
                                        }
                                        vo.setHarmId(harmId);
                                        vo.setHarmName(harmName);
                                        vo.setDiseaseName(diseaseName);
                                        if(!StringUtils.isNotNull(vo.getIdNOs())){
                                            vo.setIdNOs(new ArrayList<>());
                                            vo.setPersonNames(new ArrayList<>());
                                        }
                                        if(!vo.getIdNOs().equals(ee.getIdNo()))        {
                                            vo.getIdNOs().add(ee.getIdNo());
                                            vo.getPersonNames().add(ee.getName());
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        for (MRDiseaseExceptionVO vo:
                list) {
            Optional<OccupationalHarmAnalysisVO> oharmvo =
                    harmAnalysisVOs.stream().filter(t->t.getHarmId().equals(vo.getHarmId())).findFirst();
            if(oharmvo.isPresent()) {
                vo.setHarmEeCount(oharmvo.get().getEeCount());
            }
            if(vo.getHarmEeCount() > 0){
                double ddiseaseEeCount = vo.getPersonNames().size();
                double dEeCount = vo.getHarmEeCount();
                NumberFormat nf = NumberFormat.getPercentInstance();
                nf.setMaximumFractionDigits(2);
                String rate = nf.format(ddiseaseEeCount/dEeCount);
                vo.setExceptionRate(rate);
            }
        }
        return list;
    }
    private List<MRHarmEffectVO>  getHarmEffects(List<OccupationalHarmWorkStatusGroupVO> harmWorkstatusGroups){
        List<MRHarmEffectVO> list = new ArrayList<>();
        if(!StringUtils.isNotNull(harmWorkstatusGroups))return list;
        for (OccupationalHarmWorkStatusGroupVO hw:harmWorkstatusGroups
        ) {
            if(!StringUtils.isNotNull(hw.getContras()))continue;
            for (HarmContraCriterion harmContra:hw.getContras()
            ) {
                if(!StringUtils.isNotNull(harmContra))continue;
                for(Contraindication contra : harmContra.getContras()) {
                    if(!StringUtils.isNotNull(contra))continue;
                    if(list.stream().filter(t -> t.getHarmfactorId().equals(harmContra.getHarmId())
                            && t.getEffect().equals(contra.getContraName())
                    ).count() == 0){
                        MRHarmEffectVO vo = new MRHarmEffectVO();
                        vo.setHarmfactor(harmContra.getHarmName());
                        vo.setHarmfactorId(harmContra.getHarmId());
                        vo.setEffect(contra.getContraName());
                        list.add(vo);
                    }
                }
            }
            for (HarmDiseaseCriterion harmDiseaseCriterion:hw.getDiseaseCriterions()
            ) {
                if(!StringUtils.isNotNull(harmDiseaseCriterion))break;
                for(Disease disease : harmDiseaseCriterion.getDiseaseCriterions()) {
                    if(!StringUtils.isNotNull(disease))break;
                    if(list.stream().filter(t -> t.getHarmfactorId().equals(harmDiseaseCriterion.getHarmId())
                            && t.getEffect().equals(disease.getDiseaseName())
                    ).count() == 0){
                        MRHarmEffectVO vo = new MRHarmEffectVO();
                        vo.setHarmfactor(harmDiseaseCriterion.getHarmName());
                        vo.setHarmfactorId(harmDiseaseCriterion.getHarmId());
                        vo.setEffect(disease.getDiseaseName());
                        list.add(vo);
                    }
                }
            }
        }
        return list;

    }
    private List<MRItemLackPersonVO> getItemLackPersons(List<OccupationalCheckMainVO> checkMains){
        List<MRItemLackPersonVO> list = new ArrayList<>();
        if(!StringUtils.isNotNull(checkMains))return list;
        for (OccupationalCheckMainVO checkMainVO:checkMains
        ) {
            for(OccupationalCheckItemVO item:checkMainVO.getCheckItems()){
                if("1".equals(item.getLack())){
                    MRItemLackPersonVO vo = new MRItemLackPersonVO();
                    vo.setBcno(checkMainVO.getCheckCode());
                    vo.setIdNO(checkMainVO.getIdNo());
                    vo.setPersonName(checkMainVO.getName());
                    vo.setItemUncheck(item.getItemName());
                    list.add(vo);
                }
            }
        }
        return list;
    }
    private List<MROccupationTabooPeopleVO> getTabooPeoples(List<OccupationalEmployeeAnalysisVO> ees) {
        List<MROccupationTabooPeopleVO> list = new ArrayList<>();
        if (!StringUtils.isNotNull(ees)) return list;
        for (OccupationalEmployeeAnalysisVO ee:ees
        ) {
            if(!StringUtils.isNotNull(ee.getCheckMains())) continue;
                for (OccupationalCheckMainAnalysisVO main :
                        ee.getCheckMains()) {
                    if(!StringUtils.isNotNull(main.getConclusions()))continue;
                    if(!"1".equals(main.getConclusions().getContraindicationTag()))continue;
                    MROccupationTabooPeopleVO vo = new MROccupationTabooPeopleVO();
                    vo.setCheckCode(main.getCheckCode());
                    vo.setCheckResult(main.getConclusions().getAbnormalresult());
                    vo.setEmpName(ee.getName());
                    vo.setHarmfactor(ee.getHarmStr());
                    vo.setIdNO(ee.getIdNo());
                    vo.setMainConclusion(main.getConclusions().getConclusion());
                    vo.setSex(ee.getSex());
                    list.add(vo);
                }

        }
        return list;
    }
    private List<MRResultExceptionVO> getResultExceptions(List<OccupationalEmployeeAnalysisVO> ees,List<OccupationalHarmAnalysisVO> harmAnalysisVOs){
        List<MRResultExceptionVO> list = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
        ) {
            if(StringUtils.isNotNull(ee.getCheckMains())) {
                for (OccupationalCheckMainAnalysisVO main:
                        ee.getCheckMains()) {
                    if(!StringUtils.isNotNull(main))continue;
                    if(!StringUtils.isNotNull(main.getConclusions()))continue;
                    if(!"1".equals(main.getConclusions().getOccupationalTag()))continue;
                    for (OccupationalHarmConclusionAnalysisVO harmconclusion:ee.getHarmConclusions()
                    ) {
                        //填充禁忌症人员列表
                        if (!StringUtils.isNotNull(harmconclusion)) continue;
                        if (!StringUtils.isNotNull(harmconclusion.getConclusions())) continue;
                        if (!StringUtils.isNotNull(harmconclusion.getConclusions().getAbnormalCombGroups())) continue;
                        for (OccupationalHarmAbnormalCombVO abnormailComb :
                        harmconclusion.getConclusions().getAbnormalCombGroups()) {
                            if(!StringUtils.isNotNull(abnormailComb))continue;
                            String combId = abnormailComb.getCombId();
                            String combName = abnormailComb.getCombName();
                            String harmId = harmconclusion.getHarmId();
                            String harmName = harmconclusion.getHarmName();
                            Optional<MRResultExceptionVO> oVo = list.stream().filter(t -> harmId.equals(t.getHarmfactorId())
                            ).findFirst();
                            MRResultExceptionVO vo;
                            if(oVo.isPresent()){
                                vo = oVo.get();
                            }else{
                                vo = new MRResultExceptionVO();
                                list.add(vo);
                            }

                            vo.setHarmfactorId(harmconclusion.getHarmId());
                            vo.setHarmfactor(harmconclusion.getHarmName());
                            if(!StringUtils.isNotNull(vo.getIdNOs())){
                                vo.setIdNOs(new ArrayList<>());
                                vo.setPersonNames(new ArrayList<>());
                            }
                            if(!vo.getIdNOs().equals(ee.getIdNo()))        {
                                vo.getIdNOs().add(ee.getIdNo());
                                vo.getPersonNames().add(ee.getName());
                            }
                        }
                    }
                }
            }
        }
        for (MRResultExceptionVO vo:
                list) {
            Optional<OccupationalHarmAnalysisVO> oharmvo =
                    harmAnalysisVOs.stream().filter(t->t.getHarmId().equals(vo.getHarmfactorId())).findFirst();
            if(oharmvo.isPresent()) {
                vo.setHarmEeCount(oharmvo.get().getEeCount());
            }
            if(vo.getHarmEeCount() > 0){
                double ddiseaseEeCount = vo.getPersonNames().size();
                double dEeCount = vo.getHarmEeCount();
                NumberFormat nf = NumberFormat.getPercentInstance();
                nf.setMaximumFractionDigits(2);
                String rate = nf.format(ddiseaseEeCount/dEeCount);
                vo.setExceptionRate(rate);
            }
        }
        return list;
    }
    private List<MRSpecialRecheckPersonVO> getSpecialRecheckPersons(List<OccupationalEmployeeAnalysisVO> ees){
        List<MRSpecialRecheckPersonVO> list = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
        ) {
            if(!StringUtils.isNotNull(ee.getHarmConclusions()))continue;
            for (OccupationalHarmConclusionAnalysisVO harmConclusion:ee.getHarmConclusions()
                 ) {
                if(!StringUtils.isNotNull(harmConclusion))continue;
                if(!StringUtils.isNotNull(harmConclusion.getConclusions()))continue;
                if(!StringUtils.isNotNull(harmConclusion.getConclusions().getAbnormalCombGroups()))continue;
                    MRSpecialRecheckPersonVO vo = new MRSpecialRecheckPersonVO();
                    vo.setCheckCode(ee.getOriginCheckCode());
                    vo.setEmpName(ee.getName());
                    vo.setHarmfactor(harmConclusion.getHarmName());
                    vo.setResult(harmConclusion.getConclusions().getAbnormalDesc());
                    vo.setIdNO(ee.getIdNo());
                    list.add(vo);

            }

        }
        return list;
    }
    private List<MRSuspectedOccupationalDiseasePeopleVO> getSuspectedOccupationalDiseasePeoples(List<OccupationalEmployeeAnalysisVO> ees) {
        List<MRSuspectedOccupationalDiseasePeopleVO> list = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:ees
        ) {
            if (!StringUtils.isNotNull(ee.getCheckMains())) continue;
            if (!StringUtils.isNotNull(ee.getHarmConclusions())) continue;
            for (OccupationalCheckMainAnalysisVO checkmain:ee.getCheckMains()
                 ) {
                if (!StringUtils.isNotNull(checkmain)) continue;
                if (!StringUtils.isNotNull(checkmain.getConclusions())) continue;

                if(!"1".equals(checkmain.getConclusions().getOccupationalTag()))continue;
                MRSuspectedOccupationalDiseasePeopleVO vo;
                Optional<MRSuspectedOccupationalDiseasePeopleVO> oVo = list.stream().filter(t -> ee.getIdNo().equals(t.getIdNO())
                ).findFirst();
                if(oVo.isPresent()){
                    vo = oVo.get();
                }else{
                    vo = new MRSuspectedOccupationalDiseasePeopleVO();
                    vo.setHarmfactor(ee.getHarmStr());
                    vo.setIdNO(ee.getIdNo());
                    vo.setCheckCode(ee.getOriginCheckCode());
                    vo.setCheckResult(checkmain.getConclusions().getAbnormalresult());
                    vo.setMainConclusion(checkmain.getConclusions().getConclusion());
                    list.add(vo);
                }

                if(!StringUtils.isNotNull(vo.getDiseaseNames()))vo.setDiseaseNames(new ArrayList<>());
                for (OccupationalHarmConclusionAnalysisVO harmConclusionAnalysisVO:ee.getHarmConclusions()
                     ) {
                    if (!StringUtils.isNotNull(harmConclusionAnalysisVO)) continue;
                    if (!StringUtils.isNotNull(harmConclusionAnalysisVO.getConclusions())) continue;
                    if (!StringUtils.isNotNull(harmConclusionAnalysisVO.getConclusions().getDiseases())) continue;
                    for (OccupationalHarmDiseaseVO disease:
                    harmConclusionAnalysisVO.getConclusions().getDiseases()) {
                        if (!StringUtils.isNotNull(disease)) continue;
                        if (!StringUtils.isNotNull(disease.getDiseaseName())) continue;
                        if(!vo.getDiseaseNames().stream().anyMatch(t->disease.getDiseaseName().equals(t)))
                        {
                            vo.getDiseaseNames().add(disease.getDiseaseName());
                        }
                    }
                }
                list.add(vo);
            }

        }
        return list;
    }
    @Override
    public V_ReportVO getOccupationalReportInfo(String id) throws Exception {
        DimePhysicalMrMain mrmain = mrMainMapper.selectDimePhysicalMrMainById(id);
        if(!StringUtils.isNotNull(mrmain))throw new Exception("没有找到总检记录！");
        if(!StringUtils.isNotNull(mrmain.getJsonData()))throw new Exception("总检记录数据不正确！");

        JsonUtils<OccupationalMainVO> jsonUtils = new JsonUtils<>(OccupationalMainVO.class);
        OccupationalMainVO main = jsonUtils.jsonDeserialize(mrmain.getJsonData());
        if(!StringUtils.isNotNull(main))throw new Exception("总检记录数据不正确！");
        V_ReportVO vo = new V_ReportVO();
        vo.setReportbasicinfos(getReportBasicInfo(mrmain,main));
        vo.setReportcons(getReportcons(mrmain,main));
        vo.setReportdiseaseexceptions(getReportdiseaseexceptions(mrmain,main));
        vo.setReportexceptioncons(getReportexceptioncons(mrmain,main));
        vo.setReportdoctorlicenses(getReportDoctorLicenses(mrmain,main));
        vo.setReportdoctorsignatures(getReportDoctorSignatures(mrmain,main));
        vo.setReportexceptionress(getReportexceptionress(mrmain,main));
        vo.setReportexceptions(getReportExceptions(mrmain,main));
        vo.setReportexceptionsuss(getReportexceptionsuss(mrmain,main));
        vo.setReportfocuss(getReportfocuss(mrmain,main));
        vo.setReportharmfactorcons(getReportharmfactorcons(mrmain,main));
        vo.setReportharmfactoreffs(getReportharmfactoreffs(mrmain,main));
        vo.setReportharmfactors(getReportHarmfactors(mrmain,main));
        vo.setReportitemlacks(getReportitemlacks(mrmain,main));
        vo.setReportlabstandards(getReportlabstandards(mrmain,main));
        vo.setReportoccupationcheckorgapproves(getReportoccupationcheckorgapproves(mrmain,main));
        vo.setReportrechecks(getReportRechecks(mrmain,main));
        vo.setReportresults(getReportResults(mrmain,main));
        vo.setReportsignatures(getReportsignatures(mrmain,main));
        vo.setReportsuss(getReportsuss(mrmain,main));
        return vo;
    }
    private List<V_REPORT_BASICINFO> getReportBasicInfo(DimePhysicalMrMain mrMain,OccupationalMainVO main) throws Exception{
        List<V_REPORT_BASICINFO> re = new ArrayList<>();
        String evaluationBasis = main.getEvaluationBasis().stream().collect(Collectors.joining(","));
        List<String> listharmanalysis = main.getHarmAnalysises().stream().map(t->t.getHarmAnalysisStr()).collect(Collectors.toCollection(ArrayList::new));
        List<String> listharm = main.getHarmAnalysises().stream().map(t->t.getHarmName()).collect(Collectors.toCollection(ArrayList::new));
        V_REPORT_BASICINFO basicinfo = V_REPORT_BASICINFO.builder()
                .bcend(main.getCheckDateEnd())
                .bcstart(main.getCheckDateStart())
                .address(mrMain.getCheckAddress())
                .bcitem(main.getBcitems())
                .companyname(main.getCompany().getName())
                .crowdharmfactor(main.getCrowdHarmfactor())
                .entrustcompany(main.getDelegateCompany().getName())
                .evalutionbasis(evaluationBasis)
                .makedate(mrMain.getMakedate())
                .head("职业健康检查总结报告书")
                .orgcheck(mrMain.getOrgcheck())
                .workstatus(mrMain.getWorkstatus())
                .harmfactor(mrMain.getOrgHarmfactor())
                .persons(main.getEes().size())
                .ocptddiagnosestandard(main.getDiagnoseStandards().stream().collect(Collectors.joining(",")))
                .phoneno(main.getDelegateCompany().getPhone())
                .printlist(main.getPrintList())
                .processadvice(main.getProcessAdvice())
                .resultdesc(main.getResultDesc())
                .resultanalysis(listharmanalysis.stream().collect(Collectors.joining(",")))
                .shopcode(mrMain.getShopCode())
                .shopnamefull(mrMain.getOrgcheck())
                .signdate(mrMain.getSigndate())
                .reportno(mrMain.getReportno())
                .harmfactor(listharm.stream().collect(Collectors.joining(","))).build();
        re.add(basicinfo);
        return re;

    }
    private List<V_REPORT_RESULT> getReportResults(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_RESULT> re = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:
             main.getEes()) {
            for(OccupationalCheckMainAnalysisVO checkmain : ee.getCheckMains()) {
                V_REPORT_RESULT reportResult = V_REPORT_RESULT.builder()
                        .idno(ee.getIdNo())
                        .result(checkmain.getConclusions().getConclusion())
                        .harmfactor(ee.getHarmStr())
                        .bcno(checkmain.getCheckCode())
                        .personname(ee.getName())
                        .conclusion(checkmain.getConclusions().getConclusion())
                        .reportno(main.getReportNo())
                        .build();
                re.add(reportResult);
            }
        }
        return re;
    }
    private List<V_REPORT_EXCEPTION> getReportExceptions(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_EXCEPTION> re = new ArrayList<>();
        for (OccupationalHarmAnalysisVO harm:
                main.getHarmAnalysises()) {
            if(!StringUtils.isNotNull(harm)){
                if(harm.getDiseaseEeCount() > 0 || harm.getContraEeCount() > 0) {
                    V_REPORT_EXCEPTION reportResult = V_REPORT_EXCEPTION.builder()
                            .hamname(harm.getHarmName())
                            .harmfactorid(harm.getHarmId())
                            .mrid(mrMain.getId())
                            .reportno(mrMain.getReportno())
                            .build();
                    re.add(reportResult);
                }
            }
        }
        return re;
    }
    private List<V_REPORT_LAB_STANDARD> getReportlabstandards(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_LAB_STANDARD> re = new ArrayList<>();
        for (OccupationalCheckMainVO checkmain:
                main.getCheckMains()) {
                    for(OccupationalCheckItemVO item : checkmain.getCheckItems()) {
                        if(!StringUtils.isNotNull(item))continue;
                       if(!re.stream().anyMatch(t->t.getItemname().equals(item.getItemName()))){
                           V_REPORT_LAB_STANDARD reportResult = V_REPORT_LAB_STANDARD.builder()
                                   .sn(Integer.parseInt((StringUtils.isNotNull(item.getSn())?item.getSn().toString():"0")))
                                   .groupname(item.getGroupName())
                                   .reportno(mrMain.getReportno())
                                   .itemname(item.getItemName())
                                   .valstandard(item.getValStandard())
                                   .build();
                           re.add(reportResult);
                       }
                    }
        }
        return re;
    }
    private List<V_REPORT_HARMFACTOR> getReportHarmfactors(DimePhysicalMrMain mrMain,OccupationalMainVO main) {
        List<V_REPORT_HARMFACTOR> re = new ArrayList<>();
        for (OccupationalHarmAnalysisVO harmAnalysisVO:
                main.getHarmAnalysises()) {
            V_REPORT_HARMFACTOR reportResult = V_REPORT_HARMFACTOR.builder()
                    .harmfactor(harmAnalysisVO.getHarmName())
                    .reportno(mrMain.getReportno())
                    .mrid(mrMain.getId())
                    .build();
                re.add(reportResult);
            }

        return re;
    }
    private List<V_REPORT_RECHECK> getReportRechecks(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_RECHECK> re = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:
                main.getEes()) {
            if(!StringUtils.isNotNull(ee.getHarmConclusions()))continue;
            for (OccupationalHarmConclusionAnalysisVO harmConclusion:
                    ee.getHarmConclusions()) {
                if(!StringUtils.isNotNull(harmConclusion) || !StringUtils.isNotNull(harmConclusion.getConclusions()))continue;

                if("1".equals(harmConclusion.getConclusions().getRecheckTag())) {
                    V_REPORT_RECHECK reportResult = V_REPORT_RECHECK.builder()
                            .harmfactor(harmConclusion.getConclusions().getConclusion())
                            .reportno(mrMain.getReportno())
                            .result(harmConclusion.getConclusions().getAbnormalDesc())
                            .harmfactor(harmConclusion.getHarmName())
                            .conclusion(harmConclusion.getConclusions().getConclusion())
                            .bcno(ee.getOriginCheckCode())
                            .idno(ee.getIdNo())
                            .personname(ee.getName())
                            .specialrecheck("1")
                            .build();
                    re.add(reportResult);
                }
            }
        }
        return re;
    }
    private List<V_REPORT_DOCTORLICENSE> getReportDoctorLicenses(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_DOCTORLICENSE> re = new ArrayList<>();

        return re;
    }
    private List<V_REPORT_DOCTORSIGNATURE> getReportDoctorSignatures(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_DOCTORSIGNATURE> re = new ArrayList<>();
        V_REPORT_DOCTORSIGNATURE reportResult = new V_REPORT_DOCTORSIGNATURE();
        R<LoginUser> rsigneruser = remoteUserService.getUserInfo(mrMain.getReportsigner(), SecurityConstants.INNER);
        if(R.isSuccess(rsigneruser)){

            reportResult.setSignature(rsigneruser.getData().getSysUser().getImagesource());
            reportResult.setReportno(mrMain.getReportno());

        }
        re.add(reportResult);
        return re;
    }
    private List<V_REPORT_SIGNATURE> getReportsignatures(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_SIGNATURE> re = new ArrayList<>();
        V_REPORT_SIGNATURE reportResult = new V_REPORT_SIGNATURE();
        reportResult.setReportno(mrMain.getReportno());
        R<LoginUser> rsigner = remoteUserService.getUserInfo(mrMain.getReportsigner(), SecurityConstants.INNER);
        if(R.isSuccess(rsigner)){
            reportResult.setSignersignature(rsigner.getData().getSysUser().getImagesource());
        }
        R<LoginUser> rapprover = remoteUserService.getUserInfo(mrMain.getReportapporver(), SecurityConstants.INNER);
        if(R.isSuccess(rapprover)){
            reportResult.setApporversignature(rapprover.getData().getSysUser().getImagesource());
        }
        R<LoginUser> rmaker = remoteUserService.getUserInfo(mrMain.getReportapporver(), SecurityConstants.INNER);
        if(R.isSuccess(rmaker)){
            reportResult.setApporversignature(rmaker.getData().getSysUser().getImagesource());
        }
        re.add(reportResult);
        return re;
    }

    private List<V_REPORT_CON> getReportcons(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_CON> re = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:
                main.getEes()) {
            for (OccupationalHarmConclusionAnalysisVO harmConclusion : ee.getHarmConclusions()) {

                if ("1".equals(harmConclusion.getConclusions().getContraindicationTag())) {
                    for (OccupationalHarmContraindictionVO harmcon : harmConclusion.getConclusions().getContraindictions() ) {
                        if(!StringUtils.isNotNull(harmcon))continue;
                        V_REPORT_CON reportResult = V_REPORT_CON.builder()
                                .result(harmConclusion.getConclusions().getAbnormalDesc())
                                .bcno(harmConclusion.getConclusions().getCheckCode())
                                .reportno(mrMain.getReportno())
                                .idno(ee.getIdNo())
                                .contraindication(harmcon.getContraindicationName())
                                .personname(ee.getName())
                                .build();
                        re.add(reportResult);
                    }
                }

            }
        }
        return re;
    }

    private List<V_REPORT_SUS> getReportsuss(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_SUS> re = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:
                main.getEes()) {
            for (OccupationalHarmConclusionAnalysisVO harmConclusion : ee.getHarmConclusions()) {
                if(!StringUtils.isNotNull(harmConclusion))continue;
                if ("1".equals(harmConclusion.getConclusions().getOccupationalTag())) {
                    for (OccupationalHarmDiseaseVO harmDisease : harmConclusion.getConclusions().getDiseases()) {
                        if(!StringUtils.isNotNull(harmDisease))continue;
                        V_REPORT_SUS reportResult = V_REPORT_SUS.builder()
                                .result(harmConclusion.getConclusions().getAbnormalDesc())
                                .bcno(harmConclusion.getConclusions().getCheckCode())
                                .reportno(mrMain.getReportno())
                                .idno(ee.getIdNo())
                                .suspectedocptd(harmDisease.getDiseaseName())
                                .personname(ee.getName())
                                .build();
                        re.add(reportResult);
                    }
                }

            }
        }
        return re;
    }
    private List<V_REPORT_FOCUS> getReportfocuss(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_FOCUS> re = new ArrayList<>();
        for (OccupationalEmployeeAnalysisVO ee:
                main.getEes()) {
            for (OccupationalCheckMainAnalysisVO checkmain : ee.getCheckMains()) {
                if(!StringUtils.isNotNull(checkmain))continue;
                if("1".equals(checkmain.getConclusions().getFocusTag())){
                    V_REPORT_FOCUS reportResult = V_REPORT_FOCUS.builder()
                            .conclusion(checkmain.getConclusions().getConclusion())
                            .bcno(checkmain.getCheckCode())
                            .idno(ee.getIdNo())
                            .reportno(mrMain.getReportno())
                            .personname(ee.getName())
                            .build();
                    re.add(reportResult);
                }
            }
        }
        return re;
    }
    private List<V_REPORT_HARMFACTOR_CON> getReportharmfactorcons(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_HARMFACTOR_CON> re = new ArrayList<>();

        MRAttachmentVO attachment =  main.getAttachment();
        for (MRContradictionVO contradictions :attachment.getContradictions()
             ) {
            V_REPORT_HARMFACTOR_CON reportResult = V_REPORT_HARMFACTOR_CON.builder()
                    .contraindication(contradictions.getContradiction())
                    .harmfactor(contradictions.getHarmfactor())
                    .reportno(mrMain.getReportno())
                    .build();
            re.add(reportResult);
        }
        return re;
    }
    private List<V_REPORT_HARMFACTOR_EFF> getReportharmfactoreffs(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_HARMFACTOR_EFF> re = new ArrayList<>();

        MRAttachmentVO attachment =  main.getAttachment();
        for (MRHarmEffectVO effect :attachment.getHarmEffects()
        ) {
            V_REPORT_HARMFACTOR_EFF reportResult = V_REPORT_HARMFACTOR_EFF.builder()
                    .effect(effect.getEffect())
                    .harmfactor(effect.getHarmfactor())
                    .reportno(mrMain.getReportno())
                    .build();
            re.add(reportResult);
        }
        return re;
    }
    private List<V_REPORT_EXCEPTION_CON> getReportexceptioncons(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_EXCEPTION_CON> re = new ArrayList<>();

        MRAttachmentVO attachment =  main.getAttachment();
        for (MRContradictionExceptionVO con :attachment.getContradictionExceptions()
        ) {
            V_REPORT_EXCEPTION_CON reportResult = V_REPORT_EXCEPTION_CON.builder()
                    .contraindication(con.getContraindication())
                    .sn(0)
                    .exceptionrate(con.getExceptionRate())
                    .personlist(con.getPersonList())
                    .harmfactorid(con.getHarmId())
                    .reportno(mrMain.getReportno())
                    .build();
            re.add(reportResult);
        }
        return re;
    }
    private List<V_REPORT_EXCEPTION_RES> getReportexceptionress(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_EXCEPTION_RES> re = new ArrayList<>();

        MRAttachmentVO attachment =  main.getAttachment();
        for (MRResultExceptionVO con :attachment.getResultExceptions()
        ) {
            V_REPORT_EXCEPTION_RES reportResult = V_REPORT_EXCEPTION_RES.builder()
                    .harmfactorid(con.getHarmfactorId())
                    .sn(0)
                    .exceptionrate(con.getExceptionRate())
                    .personlist(con.getPersonList())
                    .reportno(mrMain.getReportno())
                    .itemname(con.getItemName())

                    .build();
            re.add(reportResult);
        }
        return re;
    }
    private List<V_REPORT_EXCEPTION_SUS> getReportexceptionsuss(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_EXCEPTION_SUS> re = new ArrayList<>();


        return re;
    }
    private List<V_REPORT_DISEASEEXCEPTION> getReportdiseaseexceptions(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_DISEASEEXCEPTION> re = new ArrayList<>();

        MRAttachmentVO attachment =  main.getAttachment();
        for (MRDiseaseExceptionVO con :attachment.getDiseaseExceptions()
        ) {
            V_REPORT_DISEASEEXCEPTION reportResult = V_REPORT_DISEASEEXCEPTION.builder()
                    .sn(0)
                    .exceptionrate(con.getExceptionRate())
                    .personlist(con.getPersonList())
                    .reportno(mrMain.getReportno())
                    .diseasename(con.getDiseaseName())
                    .build();
            re.add(reportResult);
        }
        return re;
    }
    private List<V_REPORT_ITEMLACK> getReportitemlacks(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_ITEMLACK> re = new ArrayList<>();
        MRAttachmentVO attachment =  main.getAttachment();
        for (MRItemLackPersonVO con :attachment.getItemLackPersons()
        ) {
            V_REPORT_ITEMLACK reportResult = V_REPORT_ITEMLACK.builder()
                    .bcno(con.getBcno())
                    .personname(con.getPersonName())
                    .idno(con.getIdNO())
                    .itemuncheck(con.getItemUncheck())
                    .reportno(mrMain.getReportno())
                    .build();
            re.add(reportResult);
        }
        return re;
    }
    private List<V_REPORT_OCCUPATIONCHECK_ORG_APPROVE> getReportoccupationcheckorgapproves(DimePhysicalMrMain mrMain,OccupationalMainVO main){
        List<V_REPORT_OCCUPATIONCHECK_ORG_APPROVE> re = new ArrayList<>();
        return re;
    }
}
